<?php

namespace app\admin\model\game;

use think\Model;


class Msgcode extends Model
{

    

    

    // 表名
    protected $name = 'game_msgcode';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'senTime_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSentimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['senTime']) ? $data['senTime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setSentimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function sendCode($tel,$code){
        $now_time = strtotime('now');
        $data = [
            "phone" => $tel,
            "code" => $code,
            "senTime" => $now_time
        ];

        $res = $this->data($data)->save();
        return $res;
    }

    public function getCode($tel){
        $where = [
            "phone" => $tel,
            "status" => 0
        ];
        $res = $this->where($where)->order("id desc")->find();
        return $res;
    }

    public function updateStatusByID($id){
        $this->where("id",$id)->update(["status"=>1]);
    }


}
